package thinkingInJava.generics.wildcard;

//: generics/GenericsAndCovariance.java
import java.util.*;

class Fruit{}
class Apple extends Fruit{}
class FApple extends Apple{}
class Jonathan extends Apple {}
class Orange extends Fruit {}

public class GenericsAndCovariance {
public static void main(String[] args) {
  // Wildcards allow covariance:
  List<? extends Fruit> flist = new ArrayList<Apple>();	// 这样的 list 完全不能改,只能调用
  // Compile Error: can't add any type of object:
  // flist.add(new Apple());
  // flist.add(new Fruit());
  // flist.add(new Object());
  flist.add(null); // Legal but uninteresting
  // We know that it returns at least Fruit:
  Fruit f = flist.get(0);
  
  List<Apple> aList = new ArrayList<Apple>();
  aList.add(new Apple());
  aList.add(new FApple());
  //aList.add(new Fruit());
  
  flist = aList;
  flist.get(0).getClass();// List<? extends Fruit> 可以这样用
}
} ///:~

